package rnd;

public class InnerIterator{
    //create an array
    private final static int SIZE = 15;
    private int[] arrayOfInts = new int[SIZE];

    public InnerIterator() {
	    //fill the array with ascending integer values
        for (int i = 0; i < SIZE; i++) {
            arrayOfInts[i] = i;
        }
    }
    
    public void printEven() {
        //print out values of even indices of the array
        InnerEvenIterator iterator = this.new InnerEvenIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.getNext() + " ");
        }
    }
    
    //inner class implements the Iterator pattern
    private class InnerEvenIterator {
        //start stepping through the array from the beginning
        private int next = 0;
        
        public boolean hasNext() {
            //check if a current element is the last in the array
            return (next <= SIZE - 1);
        }
        
        public int getNext() {
            //record a value of an even index of the array
            int retValue = arrayOfInts[next];
            //get the next even element
            next += 2;
            return retValue;
        }
    }
    
    public void printOdd(){
    	InnerOddIterator innerOddIterator = new InnerOddIterator();
    	while(innerOddIterator.hasNext()){
    		System.out.println(innerOddIterator.getNext());
    	}
    }
    
    private class InnerOddIterator{
    
    	private int next = 1;
    	
    	public Boolean hasNext(){
    		// not SIZE-1 as its OddPrinter
    		return (next<=SIZE-1);
    	}
    	
    	public int getNext(){
    		int retVal = arrayOfInts[next];
    		//get the next odd no
    		next += 2;
    		return retVal;
    	}
    	
    }
    
    public static void main(String s[]) {
        //fill the array with integer values and print out only values of even indices
        InnerIterator ds = new InnerIterator();
        System.out.println("Even");
        ds.printEven();
        System.out.println("Odd");
        ds.printOdd();
    }
}
